
#import "SearchViewController.h"
#import "HomeCell.h"
#import "EditViewController.h"

@interface SearchViewController ()
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) IBOutlet CCTextField *input;
@property (strong, nonatomic) IBOutlet UILabel *nodata;
@property (nonatomic, strong) NSMutableArray <Note *>*array;
@property (nonatomic, assign) NSInteger index;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)initUI{
    self.index = 10000000;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(16, 0, 30, 30)];
    btn.tag = 204;
    [btn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    self.input.leftViewMode = UITextFieldViewModeAlways;
    self.input.leftView = btn;
    [self.collectionView registerNib:[HomeCell nib] forCellWithReuseIdentifier:[HomeCell ID]];
    self.collectionView.c_left.equalTo(self.view);
    self.collectionView.c_right.equalTo(self.view);
    self.collectionView.c_bottom.equalTo(self.view);
    self.collectionView.c_top.equalTo(self.topView.c_bottom);
    [self.view bringSubviewToFront:self.nodata];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        [self backBtnAction];
    }
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    if (textField.text.length <= 0) {
        self.array = [NSMutableArray array];
        [self.collectionView reloadData];
        return;
    }
    [NoteTool search:textField.text complete:^(NSMutableArray<Note *> * _Nonnull result) {
        self.nodata.hidden = result.count > 0;
        self.array = [NSMutableArray arrayWithArray:result];
        [self.collectionView reloadData];
    }];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeCell ID] forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    return  cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EditViewController *vc = [EditViewController new];
    vc.model = self.array[indexPath.row];
    vc.isedit = NO;
    [self push:vc :YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 80) / 3;
    return  CGSizeMake(w, 140.0/92*w+42);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
@end
